package com.eqdd.yiqidian.widget;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.TextView;

import com.eqdd.inputs.AndroidNextInputs;
import com.eqdd.inputs.WidgetAccess;
import com.eqdd.nextinputs.StaticScheme;
import com.eqdd.yiqidian.DialogFragmentEditCustom;
import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.utils.KeyboardUtil;
import com.trello.rxlifecycle.components.support.RxAppCompatDialogFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by lvzhihao on 17-4-20.
 */

public class EditDialogFragment extends RxAppCompatDialogFragment {

    OnClickListener onClickListener;
    private AndroidNextInputs inputs;
    private WidgetAccess access;

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        String title = getArguments().getString("title");
        String content = getArguments().getString("content");
        String hint = getArguments().getString("hint");
        String content1 = getArguments().getString("content1");
        String hint1 = getArguments().getString("hint1");
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

        // Get the layout inflater
        LayoutInflater inflater = getActivity().getLayoutInflater();
        DialogFragmentEditCustom inflate = DataBindingUtil.inflate(inflater, R.layout.dialog_fragment_edit, null, false);
//        if (TextUtils.isEmpty(title)) {
//            inflate.tvTitle.setVisibility(View.GONE);
//        } else {
//            inflate.tvTitle.setText(TextUtils.isEmpty(title)?"":title);
//        }
        inflate.tvTitle.setText(TextUtils.isEmpty(title) ? "" : title);
        if (TextUtils.isEmpty(content1) && TextUtils.isEmpty(hint1)) {
            inflate.etContent.setImeOptions(EditorInfo.IME_ACTION_DONE);
        }
        inflate.etContent.setOnEditorActionListener((v, actionId, event) -> {
            if (actionId == EditorInfo.IME_ACTION_DONE) {
                ArrayList<String> strings = new ArrayList<>();
                strings.add(inflate.etContent.getText().toString().trim());
                if (!TextUtils.isEmpty(hint1)) {
                    strings.add(inflate.etContent1.getText().toString().trim());

                }
                if (inputs.test()) {
                    dismiss();
                    onClickListener.onClick(strings);
                }
            }
            return true;
        });
        inflate.etContent1.setOnEditorActionListener((v, actionId, event) -> {
            if (actionId == EditorInfo.IME_ACTION_DONE) {
                ArrayList<String> strings = new ArrayList<>();
                strings.add(inflate.etContent.getText().toString().trim());
                if (!TextUtils.isEmpty(hint1)) {
                    strings.add(inflate.etContent1.getText().toString().trim());
                }
                if (inputs.test()) {
                    dismiss();
                    onClickListener.onClick(strings);
                }
            }
            return true;
        });
        inputs = new AndroidNextInputs();
        access = new WidgetAccess(inflate.getRoot());
        inputs  // 必选，手机号
                .add(access.findEditText(R.id.et_content))
                .with(StaticScheme.Required());
        inflate.etContent.setHint(TextUtils.isEmpty(hint) ? "" : hint);
        inflate.etContent.setText(TextUtils.isEmpty(content) ? "" : content);
        if (!TextUtils.isEmpty(hint1) || !TextUtils.isEmpty(content1)) {
            inflate.etContent1.setVisibility(View.VISIBLE);
            inflate.etContent1.setHint(TextUtils.isEmpty(hint1) ? "" : hint1);
            inflate.etContent1.setText(TextUtils.isEmpty(content1) ? "" : content1);

        } else {
            inflate.etContent1.setVisibility(View.GONE);
        }
        builder.setView(inflate.getRoot())
                .setPositiveButton("确定",
                        (DialogInterface dialog, int id) -> {
                            ArrayList<String> strings = new ArrayList<>();
                            strings.add(inflate.etContent.getText().toString().trim());
                            if (!TextUtils.isEmpty(hint1)) {
                                strings.add(inflate.etContent1.getText().toString().trim());

                            }
                            if (inputs.test()) {
                                onClickListener.onClick(strings);
                            }
                        }
                ).setNegativeButton("取消", (d, p) -> {
            d.dismiss();
        });
        AlertDialog alertDialog = builder.create();
        alertDialog.setCanceledOnTouchOutside(true);
        alertDialog.setOnShowListener(dialog -> {
            inflate.etContent
                    .post(() -> {
                        inflate.etContent.requestFocus();
                        InputMethodManager imm =
                                (InputMethodManager)
                                        builder.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                        if (imm != null) {
                            imm.showSoftInput(inflate.etContent, InputMethodManager.SHOW_IMPLICIT);
                        }
                    });
        });

        return alertDialog;
    }


    @Override
    public void onDismiss(DialogInterface dialog) {
        super.onDismiss(dialog);
        KeyboardUtil.hideSoftInput(getActivity());
    }

    public void setOnClickListener(OnClickListener onClickListener) {
        this.onClickListener = onClickListener;
    }

    @Override
    public void onStart() {
        super.onStart();
        Window window = getDialog().getWindow();
        WindowManager.LayoutParams windowParams = window.getAttributes();
        windowParams.dimAmount = 0.5f;
        window.setAttributes(windowParams);
    }

    public interface OnClickListener {
        /**
         * Called when a view has been clicked.
         *
         * @param contents The view that was clicked.
         */
        void onClick(List<String> contents);
    }
}
